package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.EsabT;
import cn.e4j.modular.crm.service.EsabTService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/crm/esai120")
public class Esai120Controller extends BaseController {
    @Autowired
    private EsabTService esabTService;

    /**
     * 跳转至客户负责人维护主界面
     *
     * @return
     */
    @RequiresPermissions("crm:esai120:init")
    @RequestMapping(value = {"", "/init"})
    public String index() {
        return "/pc/crm/esai120.html";
    }

    @RequiresPermissions("crm:esai100:init")
    @RequestMapping("/getList")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();
        List<Map<String, Object>> list = esabTService.getEsablist(page, inDto);
        return new PageReData(page, list);
    }

    @RequiresPermissions("crm:esai120:add")
    @RequestMapping("/add")
    @ResponseBody
    public ResponseData add(EsabT esabT) {
        if (esabTService.existedTheParams(esabT)) {
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                    "客户编号或主要联系人重复");
        } else {
            esabTService.add(esabT);
            return SUCCESS_TIP;
        }
    }

    @RequiresPermissions("crm:esai120:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public ResponseData edit(EsabT esabT) {
        esabTService.edit(esabT);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:esai120:del")
    @RequestMapping("/del")
    @ResponseBody
    public ResponseData delRow(EsabT esabT) {
        esabTService.delete(esabT);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:esai120:del")
    @RequestMapping("/delBatch")
    @ResponseBody
    public ResponseData delRowsBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<EsabT> esabTList = inDto.getReqList(EsabT.class);
        esabTService.deleteBatch(esabTList);
        return SUCCESS_TIP;
    }

    /**
     * 根据选择的客户资料获取客户联系人资料
     */
    @RequiresPermissions("crm:esai120:del")
    @RequestMapping("/getGwalData")
    @ResponseBody
    public ResponseData getGwalData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> list = esabTService.getGwalByEsab001(inDto.getString("ESAB001"));

        return new SuccessResponseData(list.get(0));
    }

    /**
     * 查询区域资料
     */
    @RequestMapping("/getGwadList")
    @ResponseBody
    public ResponseData getGwadList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = esabTService.getGwadList(inDto);
        return ResponseData.success(result);
    }
}
